﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUUserToConnexion
    {
        /// <summary>
        /// Get All the UserToConnexion of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<UserToConnexion> GetAllUserToConnexion(bool searchInDeleted)
        {
            List<UserToConnexion> utcs = new List<UserToConnexion>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from UserToConnexion in db.UserToConnexion
                              where UserToConnexion.utc_isdeleted == searchInDeleted
                              select UserToConnexion;
                foreach (UserToConnexion tmp in results)
                    utcs.Add(tmp);
            }

            return utcs;
        }

        /// <summary>
        /// Get the UserToConnexion by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UserToConnexion GetUserToConnexionByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var utc = from UserToConnexion in db.UserToConnexion
                              where UserToConnexion.utc_id == id
                              select UserToConnexion;
                    foreach (UserToConnexion tmp in utc)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// Add a UserToConnexion into the database.
        /// </summary>
        /// <param name="utc"></param>
        /// <returns></returns>
        public static UserToConnexion AddUserToConnexion(UserToConnexion utc)
        {
            if (utc != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    UserToConnexion nUtc = db.UserToConnexion.Add(utc);
                    db.SaveChanges();
                    return nUtc;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific UserToConnexion of the database.
        /// </summary>
        /// <param name="utc"></param>
        /// <returns></returns>
        public static bool RemoveUserToConnexion(UserToConnexion utc)
        {
            if (utc != null)
            {
                utc.utc_isdeleted = true;
                if (UpdateUserToConnexion(utc) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the UserToConnexion into the database.
        /// </summary>
        /// <param name="utc"></param>
        /// <returns></returns>
        public static UserToConnexion UpdateUserToConnexion(UserToConnexion utc)
        {
            if (utc != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from UserToConnexion in db.UserToConnexion
                                  where UserToConnexion.utc_id == utc.utc_id
                                  select UserToConnexion;

                    foreach (var tmp in current)
                    {
                        tmp.usr_id = utc.usr_id;
                        tmp.utc_date_connection = utc.utc_date_connection;
                        tmp.utc_date_logout = utc.utc_date_logout;
                        tmp.utc_isdeleted = utc.utc_isdeleted;

                        db.SaveChanges();
                    }

                    utc = current as UserToConnexion;
                }
            }

            return utc;
        }

        /// <summary>
        /// Function used to close the connection for the specified user.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool CloseConnexion(User user)
        {
            if (user == null)
                return false;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var utc = from UserToConnexion in db.UserToConnexion
                              where UserToConnexion.utc_date_logout == null
                                 && UserToConnexion.usr_id == user.usr_id
                                 && UserToConnexion.utc_isdeleted == false
                              select UserToConnexion;
                    foreach (UserToConnexion tmp in utc)
                    {
                        tmp.utc_date_logout = DateTime.Now;
                    }
                    db.SaveChanges();
                    return true;
                }
            }
        }
    }
}
